import pygame
import random
import sys

# 初始化pygame
pygame.init()

# 屏幕设置
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("端午划龙舟")

# 颜色定义
BLUE = (64, 164, 223)
RED = (255, 50, 50)
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)


# 龙舟类
class DragonBoat:
    def __init__(self):
        self.width = 120
        self.height = 40
        self.x = 100
        self.y = HEIGHT // 2
        self.speed = 0
        self.max_speed = 8
        self.acceleration = 0.2
        self.deceleration = 0.1

    def draw(self):
        # 绘制龙舟主体
        pygame.draw.rect(screen, RED, (self.x, self.y, self.width, self.height))
        # 绘制龙头
        pygame.draw.polygon(screen, YELLOW, [
            (self.x + self.width, self.y + 10),
            (self.x + self.width + 30, self.y + self.height // 2),
            (self.x + self.width, self.y + self.height - 10)
        ])
        # 绘制划桨动画
        for i in range(3):
            angle = (pygame.time.get_ticks() // 200 + i) % 360
            paddle_y = self.y + 10 + i * 10
            paddle_length = 15 * (1 + 0.3 * abs(pygame.math.Vector2(1, 0).rotate(angle).y))
            pygame.draw.line(screen, WHITE,
                             (self.x + 20 + i * 30, paddle_y),
                             (self.x + 20 + i * 30 - paddle_length, paddle_y),
                             3)

    def update(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_UP]:
            self.speed = min(self.speed + self.acceleration, self.max_speed)
        elif keys[pygame.K_DOWN]:
            self.speed = max(self.speed - self.acceleration, -self.max_speed / 2)
        else:
            # 自然减速
            if self.speed > 0:
                self.speed = max(0, self.speed - self.deceleration)
            elif self.speed < 0:
                self.speed = min(0, self.speed + self.deceleration)

        self.y += self.speed
        # 边界检查
        self.y = max(50, min(HEIGHT - self.height - 50, self.y))

    def get_pos(self):
        return (self.x + self.width // 2, self.y + self.height // 2)


# 水波类
class Water:
    def __init__(self):
        self.waves = []
        for i in range(20):
            self.waves.append({
                'x': random.randint(0, WIDTH),
                'y': random.randint(0, HEIGHT),
                'size': random.randint(5, 15),
                'speed': random.uniform(0.5, 1.5)
            })

    def draw(self):
        for wave in self.waves:
            pygame.draw.circle(screen, (wave['size'] * 10, wave['size'] * 10, 255),
                               (wave['x'], wave['y']), wave['size'], 1)

    def update(self):
        for wave in self.waves:
            wave['x'] -= wave['speed']
            if wave['x'] < -20:
                wave['x'] = WIDTH + 20
                wave['y'] = random.randint(0, HEIGHT)


# 游戏主循环
def main():
    clock = pygame.time.Clock()
    boat = DragonBoat()
    water = Water()
    font = pygame.font.SysFont(None, 36)

    start_time = pygame.time.get_ticks()
    distance = 0

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        # 更新游戏状态
        boat.update()
        water.update()
        distance += boat.speed * 0.1

        # 绘制
        screen.fill(BLUE)
        water.draw()
        boat.draw()

        # 显示信息
        time_elapsed = (pygame.time.get_ticks() - start_time) // 1000
        info_text = f"时间: {time_elapsed}s  距离: {int(distance)}m  速度: {abs(boat.speed):.1f}"
        text_surface = font.render(info_text, True, WHITE)
        screen.blit(text_surface, (20, 20))

        pygame.display.flip()
        clock.tick(60)

    pygame.quit()
    sys.exit()


if __name__ == "__main__":
    main()
